<?php

namespace Biz\service\activity\project;

use Biz\model\activity\BizProjectCategoryModel;
use Biz\model\activity\BizProjectCustomerModel;
use Biz\model\activity\BizProjectGroupModel;
use Biz\model\activity\BizProjectModel;
use Biz\model\activity\BizProjectResponsibilityModel;
use Doraemon\model\Admin\BeerUsUser as AdminUsers;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;


class BizProjectProcess extends BizProjectPageTpl
{

    const TPL_CONFIG_TPL_NAME = '项目跟进情况';
    public float $total_project_money = 0;
    public float $total_project_count = 0;


    public function condition(): array
    {
        $fieldAuth = BizProjectGroup::fieldAuth($this->adminId(), 'admin');
        if (!$this->execForm($fieldAuth['project_process'])) throw new Exception("权限不足");
        $project_start_date = $this->getRequest()->input('project_date.start', '');
        $project_end_date = $this->getRequest()->input('project_date.end', '');
        $created_at_start_date = $this->getRequest()->input('created_at.start', '');
        $created_at_end_date = $this->getRequest()->input('created_at.end', '');
        $process_state = $this->getRequest()->input('process_state', '');
        $responsibility = $this->getRequest()->input('responsibility', '');
        $condition = [];
        if ($project_start_date && $project_end_date) {
            $condition['OR #project_date'] = [
                'project_start_date[<>]' => [$project_start_date, $project_end_date],
                'project_end_date[<>]' => [$project_start_date, $project_end_date],
                'AND' => [
                    'project_start_date[<]' => $project_start_date,
                    'project_end_date[>]' => $project_end_date,
                ]
            ];
        } elseif ($project_start_date) {
            $condition['project_end_date[>=]'] = $project_start_date;
        } elseif ($project_end_date) {
            $condition['project_start_date[<=]'] = $project_end_date;
        }
        if ($created_at_start_date && $created_at_end_date) {
            $condition['created_at[<>]'] = [$created_at_start_date, $created_at_end_date];
        } elseif ($created_at_start_date) {
            $condition['created_at[>=]'] = $created_at_start_date;
        } elseif ($created_at_end_date) {
            $condition['created_at[<=]'] = $created_at_end_date;
        }
        if ($process_state) $condition['process_state'] = $process_state;
        if ($responsibility) $condition['responsibility'] = $responsibility;
        return $condition;
    }

    public function withInfoHtml(): array
    {
        $html = [
            "项目金额总计 : " . (number_format($this->total_project_money, 2)),
            "项目总数 : " . $this->total_project_count,
        ];
        return ["提示", implode("<br/>", $html)];
    }

    public function getList(): array
    {
        $condition = ['ORDER' => ['id' => 'DESC']];
        //'amount', 'response', 'count'
        extract(BizProjectModel::getProcessList( $this->getRequestCondition() + $condition));
        $this->total_project_money = $amount;
        $this->total_project_count = $count;
        return $response;
    }

    public function getTotal(): int
    {
        return 0;
    }

    public function getGrid(Grid $grid)
    {
        $grid->disablePagination();
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->between('project_date', '项目周期')->date();
                $filter->equal('responsibility', '项目责任方')->multipleSelect(BizProjectResponsibilityModel::RESPONSIBILITY_TYPE_TITLE_MAP);
            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->between('created_at', '创建时间')->datetime();
                $filter->equal('process_state', '项目状态')->multipleSelect(BizProjectProject::PROCESS_STATE_MAP);
            });
            if ($this->getRequestCondition()) $filter->expand();
        });
        $grid->column('responsibility', '项目责任方');
        $grid->column('num', '项目数量');
        $grid->column('amount', '项目金额');
    }

    public function getForm(Form $form)
    {
    }

    public function getFormInfo($id)
    {
    }

    /**
     * @throws Exception
     */
    public function save($input)
    {

    }

    /**
     * @throws Exception
     */
    public function delete($id)
    {

    }

    public function breadcrumb(): array
    {
        return [
            [
                'text' => BizProjectProject::getListTitle(), 'url' => BizProjectProject::getRoute()->getListRoute()
            ],
        ];
    }
}